<?php
/**
 * 客源类
 *
 * @version 	0.0.1
 */

class Customermodel extends Model 
{
	
	// ------------------------------------------------------------------------
	/**
	 * initialises the class inheriting the methods of the class Model 
	 *
	 * @return Systemparam
	 */
    function Customermodel()
    {
        parent::Model();
    }
	
    // ------------------------------------------------------------------------
    
    /**
     * 搜索客源信息，参数为条件数组
     * 
     * @return 记录集
     */
    function search($condition, $count = false)
    {
	    if ($count==true)
	    {
	    	$this->db->select('count(id) as count');
	    }
	    else
	    {
	    	$this->db->select('*');
	    }
	    if (!empty($condition['id']))
	    {
	    	$this->db->where('id', $condition['id']);
	    }
	    // 求租求购 类型
	    if (!empty($condition['searchType']))
	    {
	    	$this->db->where('if_rent', $condition['searchType']);
	    }
	    // 用途
	    if (!empty($condition['purpose']))
	    {
	    	$this->db->where('PURPOSE', $condition['purpose']);
	    }
	    // 客户姓名
	    if (!empty($condition['req_name']))
	    {
			$this->db->like('req_name', $condition['req_name']);
	    }
	    // 价格1
	    if (!empty($condition['price_down']))
	    {
	    	$this->db->where('price_down >=', $condition['price_down']);
	    }
	    // 价格2
	    if (!empty($condition['price_up']))
	    {
	    	$this->db->where('price_up <=', $condition['price_up']);
	    }
	    // 户型1
	    if (!empty($condition['house_typedown']))
	    {
	    	$this->db->where('house_typedown >=', $condition['house_typedown']);
	    }
	    // 户型2
	    if (!empty($condition['house_typeup']))
	    {
	    	$this->db->where('house_typeup <=', $condition['house_typeup']);
	    }

	    // 分页显示
	    if ($count==false && isset($condition['start']) && isset($condition['limit']))
	    {
			$this->db->limit($condition['limit'], $condition['start']);
	    }

	    $query = $this->db->get('req_baseinfo');

        if ($query->num_rows() > 0)
        {
            return $query->result();
        }
    	return ;
    }
    
    // 根据ID获取客源记录，返回记录集
    function getCustomerById($id)
    {
        //$this->db->select('*');
        $this->db->where('id', $id);
        $query = $this->db->get('req_baseinfo');
        
        if ($query->num_rows() > 0)
        {
            return $query->row();
        }
    	return ;
    }

	// 新增客源
	function add($param)
	{
	    $obj =& get_instance();
	    $obj->load->model('Districtmodel');

        $row = $obj->Districtmodel->getDistrictById($param['city_zone']);
		$city_zone = $row->NAME;
		if ($city_zone=='')
		{
			$city_zone = '不限';
		}

		//print_r($param);

		foreach ($param as $key => $value)
		{
			if ($value!='')
			{
				$data[$key] = $value;
			}
		}

		$data['city_zone'] = $city_zone;
		$data['input_date'] = date('Y-m-d H:i:s');
		$data['input_userid'] = $param['duty_userid'];

		$this->db->insert('req_baseinfo', $data);

		return true;
	}

	// 编辑客源
	function edit($param)
	{
	    $obj =& get_instance();
	    $obj->load->model('Districtmodel');

        $row = $obj->Districtmodel->getDistrictById($param['city_zone']);
		$city_zone = $row->NAME;
		if ($city_zone=='')
		{
			$city_zone = '不限';
		}

		$data['kh_secret'] = 0;
		$data['ky_secret'] = 0;
		$data['kd_secret'] = 0;

		foreach ($param as $key => $value)
		{
			if ($value!='')
			{
				$data[$key] = $value;
			}
		}

		// 编辑时，id即为id
		$id = $param['id'];
		$data['city_zone'] = $city_zone;
		$data['input_date'] = date('Y-m-d H:i:s');
		$data['input_userid'] = $param['duty_userid'];
		

		// 所有需要的变量设置完毕
		// 移除非数据库字段的Key，这一步一定要注意
		// 不需要更新字段id，所以也一并移除
		unset($data['id']);

		$this->db->where('id', $id);
		$this->db->update('req_baseinfo', $data); 

		return true;
	}

	// 删除客源
	function delete($param)
	{
		foreach ($param as $value)
		{
			if (is_numeric($value))
			{
				$this->db->orwhere('id', $value);
			}
		}
		$this->db->delete('req_baseinfo');

		return true;
	}
}